/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.business;

import android.app.Activity;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.entity.mainbusiness.ActionShowMainItemBean;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.MathUtils;
import com.naizhuai.util.NaizhuAILog;


import java.util.List;

public class ActionMainCurrentAdapterHolder extends RecyclerView.Adapter<ActionMainCurrentAdapterHolder.ActionItemDetailsHolder> {

    private OnItemClickListener mItemClickListener;
    private Activity mContext;
    private List<ActionShowMainItemBean> actionList;
    private static int TIME_DURATION = 20;
    private static int TIME_INTERVAL = 18;
    private int type;


    public ActionMainCurrentAdapterHolder(Activity context, List<ActionShowMainItemBean> actionList, int type) {
        this.mContext = context;
        this.actionList = actionList;
        this.type = type;
    }


    @Override
    public int getItemCount() {
        return actionList.size();
    }

    @NonNull
    @Override
    public ActionItemDetailsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_action_main_image_details, parent, false);
        return new ActionItemDetailsHolder(sView);
    }


    @Override
    public void onBindViewHolder(@NonNull ActionItemDetailsHolder imageHolder, int position) {
        ActionShowMainItemBean actionItemBean = actionList.get(position);
        ImageUtils.glideThumbViewByUrl(mContext, imageHolder.chargeThumb, actionItemBean.getCreateUserInfo().getThumb());
        imageHolder.actionStatus.setText(actionItemBean.getQuestionStatusName());
        ImageUtils.glideImageViewByUrl(mContext, imageHolder.imageContent, actionItemBean.getQuestionImageUrl());
        imageHolder.actionName.setText(actionItemBean.getQuestionTitle());
        double proRadius = ActionShowMainItemBean.getProcess(actionItemBean.getStep());
        double radiusUp = MathUtils.numberSubstrationReNUmber(System.currentTimeMillis()/1000 + "", actionItemBean.getQuestionStartTime());
        double radiusDown = MathUtils.numberSubstrationReNUmber(actionItemBean.getQuestionEndTime(), actionItemBean.getQuestionStartTime());
        NaizhuAILog.e("EndTime=" + actionItemBean.getQuestionEndTime() + ";StartTime=" + actionItemBean.getQuestionStartTime() + ";currentTime=" + System.currentTimeMillis()/1000);
        NaizhuAILog.e("radiusUp=" + radiusUp + ";radiusDown=" + radiusDown);
        double timeRadius = radiusUp / radiusDown;
        NaizhuAILog.e("action_timeRadius=" + timeRadius);
        imageHolder.itemView.post(new Runnable() {
            @Override
            public void run() {
                int totalWidth = imageHolder.processTotal.getWidth();
                int proTotalWidth = (int) (totalWidth * proRadius);
                int timeTotalWidth = (int) (totalWidth * timeRadius);
                int processWidth = 3;
                int timeWidth = 3;
                if (processWidth < proTotalWidth) {
                    new Handler().post(new AddRunnable(proTotalWidth, processWidth, (proTotalWidth - processWidth) / TIME_DURATION, imageHolder.actionProcess));
                }
                if (timeWidth < timeTotalWidth) {
                    new Handler().post(new AddRunnable(timeTotalWidth, timeWidth, (timeTotalWidth - timeWidth) / TIME_DURATION, imageHolder.timeProcess));
                }
            }
        });
    }

    class AddRunnable implements Runnable {
        private int total;
        private int source;
        private int interAdd;
        private View mView;

        public AddRunnable(int total, int source, int interAdd, View mView) {
            this.total = total;
            this.source = source;
            this.interAdd = interAdd;
            this.mView = mView;
        }

        @Override
        public void run() {
//            NaizhuAILog.e("start.............");
            if (source < total) {
                source += interAdd;
                source = source > total ? total : source;
            } else
                return;
            if (source <= total) {
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mView.getLayoutParams();
                params.width = source;
                mView.setLayoutParams(params);
                new Handler().postDelayed(new AddRunnable(total, source, interAdd, mView), TIME_INTERVAL);
            }
        }
    }


    public class ActionItemDetailsHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        CircleImageView chargeThumb;
        TextView actionStatus, actionName;
        ImageView imageContent;
        View processTotal, actionProcess, timeProcess;

        public ActionItemDetailsHolder(View view) {
            super(view);
            chargeThumb = view.findViewById(R.id.action_charge_thumb);
            actionStatus = view.findViewById(R.id.action_status);
            actionName = view.findViewById(R.id.action_name);
            imageContent = view.findViewById(R.id.action_item_image_content);
            processTotal = view.findViewById(R.id.total_process_line);
            actionProcess = view.findViewById(R.id.process_line);
            timeProcess = view.findViewById(R.id.time_line);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null)
                mItemClickListener.onItemClick(v, getAdapterPosition(), type);
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position, int type);
    }

    public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

}
